import { Component, OnInit, ViewChild } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { _HttpClient } from '@delon/theme';
import { SFSchema, SFUISchema } from '@delon/form';
import { of } from 'rxjs';
import { delay } from 'rxjs/operators';

@Component({
  selector: 'app-archive-file-edit',
  templateUrl: './edit.component.html',
})
export class ArchiveFileEditComponent implements OnInit {
  record: any = {};
  i: any;
  schema: SFSchema = {
    properties: {
      no: { type: 'string', title: '编号' },
      title: { type: 'string', title: '文件标题' },
      code: { type: 'string', title: '文号' },
      orange: { type: 'string', title: '来文单位' },
      receipt: { type: 'string', title: '收文时间' },
      dese: { type: 'string', title: '密级' },
      urgency: { type: 'string', title: '紧急程度' },
      status: { type: 'string', title: '状态', enum:['在库', '借出'], },
      opinion: { type: 'string', title: '拟办意见' },
      remarks: { type: 'string', title: '备注' },
      nstructions: { type: 'string', title: '领导批示' },
    },
    required: ['title', 'code', 'orange'],
  };
  ui: SFUISchema = {
    '*': {
      spanLabelFixed: 100,
      grid: { span: 12 },
    },
    $no: {
      widget: 'string'
    },
    $receipt: {
      widget: 'date'
    },
    $status: {
      widget: 'radio',
      
    },
    $dese: {
      widget: 'select',
      asyncData: () => of([
        {
          label: '密级',
          group: true,
          children: [
            { label: '机密', value: '机密' },
            { label: '普通', value: '普通' },
          ]
        }
      ]).pipe(delay(100))
    },
    $urgency: {
      widget: 'select',
      asyncData: () => of([
        {
          // label: '紧急程度',
          group: true,
          children: [
            { label: '紧急', value: '紧急' },
            { label: '普通', value: '普通' },
          ]
        }
      ]).pipe(delay(100))
    },
    $opinion: {
      widget: 'textarea',
      grid: { span: 24 },
      autosize: { minRows: 1, maxRows: 6 },
    },
    $nstructions: {
      widget: 'textarea',
      grid: { span: 24 },
      autosize: { minRows: 1, maxRows: 6 },
    },
    $remarks: {
      widget: 'textarea',
      grid: { span: 24 },
      autosize: { minRows: 1, maxRows: 6 },
    },
  };

  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public http: _HttpClient,
  ) { }

  ngOnInit(): void {
    if (this.record.id > 0)
      this.http.get(`/archive/edit?id=${this.record.id}`).subscribe(res => (this.i = res));
  }

  save(value: any) {
    this.http.post(`/archive/save`, value).subscribe(res => {
      this.msgSrv.success('保存成功');
      this.modal.close(true);
    });
  }

  close() {
    this.modal.destroy();
  }
}
